package com.shaun.tea.controller;


import com.shaun.tea.config.OperatePicUtils;
import com.shaun.tea.entity.AddProductInfo;
import com.shaun.tea.entity.SmallProduct;
import com.shaun.tea.entity.Product;
import com.shaun.tea.mapper.SmallProductMapper;
import com.shaun.tea.mapper.ProductMapper;
import com.shaun.tea.service.SmallProductService;
import com.shaun.tea.service.ProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private SmallProductMapper smallproductMapper;
    @Autowired
    private SmallProductService smallproductService;

    @RequestMapping(value = "/userQuerysmallproductByProductId/{code}",method = RequestMethod.POST)
    public String userQuerysmallproductByProductId(@RequestParam("code") String code, Model model) {
        QueryWrapper<SmallProduct> queryWrapper =new QueryWrapper<>();
        queryWrapper
                .eq("smallproduct_code",code)
                .or().eq("product_code",code);
        List<SmallProduct> smallproducts = smallproductMapper.selectList(queryWrapper);
        model.addAttribute("smallproductList",smallproducts);
        return "seller/smallproductListTouser.html";
    }

    @GetMapping(value = "/manageProduct")
    public String manageProductList() {
        return "admin/manageProduct";
    }
    @GetMapping(value = "/sellerProduct")
    public String sellerProductList() {
        return "seller/sellerProduct";
    }

    @GetMapping(value = "/productList/{page}")
    public String adminProductList(@PathVariable("page") Integer page, Model model) {
        Page returnPage = productService.page(new Page(page, 8));
        model.addAttribute("productList", returnPage.getRecords());
        model.addAttribute("pages", returnPage.getPages());
        model.addAttribute("current", returnPage.getCurrent());
        model.addAttribute("total", returnPage.getTotal());
        model.addAttribute("hasPrevious", returnPage.hasPrevious());
        model.addAttribute("hasNext", returnPage.hasNext());
        return "admin/productList";
    }

    @GetMapping(value = "/userProductList/{page}")
    public String userProductList(@PathVariable("page") Integer page, Model model) {
        Page returnPage = productService.page(new Page(page, 8));
        model.addAttribute("productList", returnPage.getRecords());
        model.addAttribute("pages", returnPage.getPages());
        model.addAttribute("current", returnPage.getCurrent());
        model.addAttribute("total", returnPage.getTotal());
        model.addAttribute("hasPrevious", returnPage.hasPrevious());
        model.addAttribute("hasNext", returnPage.hasNext());
        return "seller/productList";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/addProduct")
    public ModelAndView addProduct(@RequestParam("file") MultipartFile file, AddProductInfo productDTO) throws Exception {
        Product product = new Product();
        String fileName = OperatePicUtils.uploadPic(file);
        BeanUtils.copyProperties(productDTO, product);
        product.setPImg(fileName);
        productService.save(product);
        List<SmallProduct> smallproducts = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            SmallProduct smallproduct = new SmallProduct(product.getPCode());
            smallproducts.add(smallproduct);
        }
        smallproductService.saveBatch(smallproducts);
        return new ModelAndView(new RedirectView("/product/productList/1"));
    }

    @GetMapping("/toEditProduct/{code}")
    public String toEditProduct(@PathVariable("code") String code, Model model) {
        Product product = productService.getOne(new QueryWrapper<Product>().eq("product_code", code));
        model.addAttribute("product", product);
        return "admin/editProduct";
    }

    @PostMapping("/editProduct/{code}")
    public ModelAndView editProduct(@RequestParam("file") MultipartFile file, @PathVariable("code") String code, AddProductInfo editProductDTO) throws Exception {
        Product product = productService.getOne(new QueryWrapper<Product>().eq("product_code", code));
        if (file.getOriginalFilename() != null && !file.getOriginalFilename().equals("")) {
            String fileName = OperatePicUtils.uploadPic(file);
            product.setPImg(fileName);
        }
        BeanUtils.copyProperties(editProductDTO, product);
        productService.update(product, new QueryWrapper<Product>().eq("product_code", product.getPCode()));
        return new ModelAndView(new RedirectView("/product/productList/1"));
    }

    @GetMapping("/getsmallproductByProductId/{code}")
    public String getsmallproductByProductId(@PathVariable("code") String code, Model model) {
        List<SmallProduct> smallproducts = smallproductService.list(new QueryWrapper<SmallProduct>()
                .eq("product_code", code)
                .eq("smallproduct_status",1)
        );
        model.addAttribute("smallproductList", smallproducts);
        return "admin/smallproductList";
    }

    @GetMapping("/userGetsmallproductByProductId/{code}")
    public String userGetsmallproductByProductId(@PathVariable("code") String code, Model model) {
        List<SmallProduct> smallproducts = smallproductService.list(new QueryWrapper<SmallProduct>().eq("product_code", code));
        model.addAttribute("smallproductList", smallproducts);
        return "seller/smallproductList";
    }


    @GetMapping("/toAddPage")
    public String toAddPage() {
        return "admin/addProduct";
    }

    @GetMapping("/toWelcome")
    public String toWelcome() {
        return "seller/welcome";
    }

    @GetMapping("/sold/{code}")
    public ModelAndView sold(@PathVariable("code") String code) {
        SmallProduct findsmallproduct = smallproductService.getOne(new QueryWrapper<SmallProduct>().eq("smallproduct_code", code));
        Product findProduct = productService.getOne(new QueryWrapper<Product>().eq("product_code", findsmallproduct.getPCode()));
        findsmallproduct.setSStatus(1);
        findsmallproduct.setSaleTime(new Date());
        findProduct.setPStatus(1);
        smallproductService.update(findsmallproduct, new QueryWrapper<SmallProduct>().eq("smallproduct_code", code));
        productService.update(findProduct, new QueryWrapper<Product>().eq("product_code", findsmallproduct.getPCode()));
        return new ModelAndView(new RedirectView("/product/userGetsmallproductByProductId/" + findsmallproduct.getPCode()));
    }

}
